/* Esportazione di richieste all'interno di un periodo */
DEFINE TEMP-TABLE ttRichiesta
  FIELD aktienr LIKE aktie.aktienr
  FIELD omschrijving LIKE aktie.omschrijving
  FIELD minuten      LIKE besteding.minuten FORMAT "zzz9"
  FIELD uren         AS INTEGER
  FIELD dagen        AS DECIMAL
INDEX indAktie IS UNIQUE aktienr.

DEFINE VARIABLE cFileName AS CHARACTER  NO-UNDO.
DEFINE VARIABLE iElement  AS INTEGER    NO-UNDO.
DEFINE VARIABLE lOk       AS LOGICAL    NO-UNDO.

{sy/i/generico.i}

/* FUNCTIONS */
FUNCTION Hours RETURNS DECIMAL
  ( /* parameter-definitions */ 
    INPUT ipiMinutes AS INTEGER
  ) :
/*------------------------------------------------------------------------------
  Purpose:  Returns number of hours (truncated to 15 minutes)
    Notes:  
------------------------------------------------------------------------------*/
DEFINE VARIABLE deHours AS DECIMAL    NO-UNDO.

   deHours = TRUNC(ipiMinutes / 15, 0) * 0.25.

   RETURN deHours.

END FUNCTION.

FOR EACH besteding NO-LOCK
WHERE zoeknaam = "COMUNEMI"
AND   datum   GT 7/01/2006
AND   klantref = "",
FIRST aktie OF besteding NO-LOCK
WHERE aktie.module = "mi"
BREAK
BY besteding.aktienr:
  IF FIRST-OF(besteding.aktienr) THEN DO:
    CREATE ttRichiesta.
    ASSIGN
      ttRichiesta.aktienr = besteding.aktienr
    .
    DO iElement = 1 TO 8:
      ASSIGN
        ttRichiesta.omschrijving[iElement] = aktie.omschrijving[iElement]
      .
    END.
  END.
  FIND ttRichiesta
  WHERE ttRichiesta.aktienr = besteding.aktienr.
  ASSIGN
    ttRichiesta.minuten = ttRichiesta.minuten + besteding.minuten
  .
END.

FOR EACH ttRichiesta:
  ASSIGN
    ttRichiesta.uren = Hours(ttRichiesta.minuten)
    ttRichiesta.dagen = ttRichiesta.uren / 8
  .
END.


cFileName = "C:\Documents and Settings\Wim van der Ham\Documenti\Comune MI\Rapportini\Richieste lavorate.csv".
RUN salvaCSV
   (INPUT cFileName,
    INPUT TEMP-TABLE ttRichiesta:HANDLE).


lOk = DYNAMIC-FUNCTION('openFile':U, cFileName).
IF lOk = FALSE THEN
   MESSAGE "Couldn't open" SKIP
   cFileName
      VIEW-AS ALERT-BOX INFO BUTTONS OK.

PROCEDURE salvaCSV:
/*------------------------------------------------------------------------------
  Purpose:     Salva un temp-table in formato CSV
  Parameters:  input ipcFileName
               input iphTT handle to temp-table
  Notes:       
------------------------------------------------------------------------------*/
DEFINE INPUT  PARAMETER ipcFileName AS CHARACTER  NO-UNDO.
DEFINE INPUT  PARAMETER iphTT       AS HANDLE     NO-UNDO.

DEFINE VARIABLE hBuffer      AS HANDLE     NO-UNDO.
DEFINE VARIABLE hQuery       AS HANDLE     NO-UNDO.
DEFINE VARIABLE hField       AS HANDLE     NO-UNDO.
DEFINE VARIABLE iField       AS INTEGER    NO-UNDO.
DEFINE VARIABLE cLabel       AS CHARACTER  NO-UNDO.
DEFINE VARIABLE cValue       AS CHARACTER  NO-UNDO.
DEFINE VARIABLE iIndex       AS INTEGER    NO-UNDO.
DEFINE VARIABLE cDataColumns AS CHARACTER  NO-UNDO.

   CREATE BUFFER hBuffer FOR TABLE iphTT:DEFAULT-BUFFER-HANDLE.

   cDataColumns = DYNAMIC-FUNCTION('getPrivate':U, iphTT, "DataColumns").
   IF cDataColumns = "" THEN DO:
      DO iField = 1 TO hBuffer:NUM-FIELDS:
         IF iField GT 1 THEN DO:
            cDataColumns = cDataColumns + ",".
         END.
         hField = hBuffer:BUFFER-FIELD(iField).
         cDataColumns = cDataColumns +
            hField:NAME.
      END.
   END.

   OUTPUT TO VALUE(ipcFileName).
   FieldBlock:
   DO iField = 1 TO NUM-ENTRIES(cDataColumns):
      hField = hBuffer:BUFFER-FIELD(ENTRY(iField, cDataColumns)).
      IF hField:EXTENT GT 0 THEN DO:
         /* Array field */
         DO iIndex = 1 TO hField:EXTENT:
            cLabel = "".
            IF NUM-ENTRIES(hField:LABEL) GE iIndex THEN DO:
               cLabel = ENTRY(iIndex, hField:LABEL).
            END.
            ELSE DO:
               cLabel = hField:LABEL.
            END.
            IF hField:COLUMN-LABEL NE ? THEN DO:
               cLabel = cLabel + " " + hField:COLUMN-LABEL.
            END.
            cLabel = REPLACE(cLabel, "!", " ").
            PUT UNFORMATTED
               cLabel.
            IF  iIndex = hField:EXTENT 
            AND iField = NUM-ENTRIES(cDataColumns) THEN DO:
               PUT UNFORMATTED
                  SKIP.
            END.
            ELSE DO:
               PUT UNFORMATTED
                  ";".
            END.
         END.
      END. /* Array field */
      ELSE DO:
         IF hField:COLUMN-LABEL NE ? THEN DO:
            cLabel = hField:COLUMN-LABEL.
         END.
         ELSE DO:
            cLabel = hField:LABEL.
         END.
         cLabel = REPLACE(cLabel, "!", " ").
         PUT UNFORMATTED
            cLabel.
         IF iField = NUM-ENTRIES(cDataColumns) THEN DO:
            PUT UNFORMATTED
               SKIP.
         END.
         ELSE DO:
            PUT UNFORMATTED
               ";".
         END.
      END.
   END.

   CREATE QUERY hQuery.
   hQuery:SET-BUFFERS(hBuffer).
   hQuery:QUERY-PREPARE(SUBSTITUTE("for each &1", hBuffer:NAME)).
   hQuery:QUERY-OPEN().
   hQuery:GET-FIRST().
   DO WHILE hBuffer:AVAILABLE:
      ExportBlock:
      DO iField = 1 TO NUM-ENTRIES(cDataColumns):
         hField = hBuffer:BUFFER-FIELD(ENTRY(iField, cDataColumns)).
         IF hField:EXTENT GT 0 THEN DO:
            /* Array field */
            DO iIndex = 1 TO hField:EXTENT:
               IF hField:DATA-TYPE = "DATE" THEN DO:
                  IF hField:BUFFER-VALUE(iIndex) NE ? THEN DO:
                     PUT UNFORMATTED 
                        hField:BUFFER-VALUE(iIndex).
                  END.
               END.
               ELSE DO:
                  IF hField:DATA-TYPE = "CHARACTER" THEN DO:
                     IF INDEX(hField:BUFFER-VALUE(iIndex), '"') NE 0 THEN DO:
                        /* Replace quote characters with double quotes */
                        cValue = REPLACE(hField:BUFFER-VALUE(iIndex), '"', '""').
                     END.
                     ELSE DO:
                        cValue = hField:BUFFER-VALUE(iIndex).
                     END.
                     PUT UNFORMATTED
                        '"' cValue '"'.
                  END.
                  ELSE DO:
                     PUT UNFORMATTED
                        hField:BUFFER-VALUE(iIndex).
                  END.
               END.
               IF  iIndex = hField:EXTENT 
               AND iField = NUM-ENTRIES(cDataColumns) THEN DO:
                  PUT UNFORMATTED
                     SKIP.
               END.
               ELSE DO:
                  PUT UNFORMATTED
                     ";".
               END.
            END.
         END. /* Array field */
         ELSE DO:
            IF hField:DATA-TYPE = "DATE" THEN DO:
               IF hField:BUFFER-VALUE NE ? THEN DO:
                  PUT UNFORMATTED 
                     hField:BUFFER-VALUE.
               END.
            END.
            ELSE DO:
               IF hField:DATA-TYPE = "CHARACTER" THEN DO:
                  IF INDEX(hField:BUFFER-VALUE, '"') NE 0 THEN DO:
                     /* Replace quote characters with double quotes */
                     cValue = REPLACE(hField:BUFFER-VALUE, '"', '""').
                  END.
                  ELSE DO:
                     cValue = hField:BUFFER-VALUE.
                  END.
                  PUT UNFORMATTED
                     '"' cValue '"'.
               END.
               ELSE DO:
                  PUT UNFORMATTED
                     hField:BUFFER-VALUE.
               END.
            END.
            IF iField = NUM-ENTRIES(cDataColumns) THEN DO:
               PUT UNFORMATTED
                  SKIP.
            END.
            ELSE DO:
               PUT UNFORMATTED
                  ";".
            END.
         END.
      END.
      hQuery:GET-NEXT().
   END.

   DELETE OBJECT hBuffer.
   DELETE OBJECT hQuery.

   IF iphTT:PRIVATE-DATA NE ? THEN DO:
      RUN exportPrivate
         (INPUT iphTT:PRIVATE-DATA).
   END.

   OUTPUT CLOSE.



END PROCEDURE.




